package com.njrsrc.android.tasks;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.njrsrc.android.bean.Company;
import com.njrsrc.android.bean.JobInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 工作详情
 * 
 * @author Administrator
 * 
 */
public class JobDetailTask extends AsyncTask<Object, Integer, Void> {
	private String url;
	private Handler mHandler;
	private UserInfo userInfo;
	private JobInfo jobInfo;
	private Company company;

	@Override
	protected Void doInBackground(Object... params) {
		if (params.length < 4) {
			return null;
		}
		mHandler = (Handler) params[0];
		url = (String) params[1];
		userInfo = (UserInfo) params[2];
		jobInfo = (JobInfo) params[3];
		JSONObject jsonObject = Helpers.getJSONArrayByGet(url);
		Message msg = mHandler.obtainMessage();
		try {
			if (jsonObject == null) {
				msg.what = MsgID.JOB_DETAIL_ERROR;
			} else if (jsonObject.toString().contains("error_code")) {
				// 返回错误
				userInfo.setError_code(jsonObject.getString("error_code"));
				userInfo.setError_message(jsonObject.getString("error_message"));
				msg.what = MsgID.JOB_DETAIL_FAIL;
			} else {
				msg.what = MsgID.JOB_DETAIL_SUCCESS;
				jobInfo.setJob_id(jsonObject.getString("job_id"));
				jobInfo.setTitle(jsonObject.getString("title"));
				jobInfo.setModified_at(jsonObject.getString("modified_at"));
				jobInfo.setNeed_number(jsonObject.getString("need_number"));
				jobInfo.setEducation(jsonObject.getString("education"));
				jobInfo.setWork_age(jsonObject.getString("work_age"));
				jobInfo.setArea(jsonObject.getString("area"));
				jobInfo.setWork_type(jsonObject.getString("work_type"));
				jobInfo.setDescription(jsonObject.getString("description"));
				jobInfo.setRequirement(jsonObject.getString("requirement"));
				jobInfo.setOther(jsonObject.getString("other"));
				jobInfo.setSalary(jsonObject.getString("salary"));
				jobInfo.setIs_favorite(jsonObject.getBoolean("is_favorite"));
				jobInfo.setJob_fav_id(jsonObject.getString("job_fav_id"));
				jobInfo.setIs_applied(jsonObject.getBoolean("is_applied"));
				// 公司
				JSONObject companyObject = jsonObject.getJSONObject("company");
				company = new Company();
				company.setAddress(companyObject.getString("address"));
				company.setCompany_id(companyObject.getString("company_id"));
				company.setLogo_url(companyObject.getString("logo_url"));
				company.setName(companyObject.getString("name"));
				jobInfo.setCompany(company);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mHandler.sendMessage(msg);
		return null;
	}
}